import { ajax } from "./ajax.js";
import {getFormData} from "./checkChangePasswordForm.js";
var submit = document.querySelector(".btn");//提交按钮
var emailInput = document.querySelector("#exampleInputEmail1");//邮箱输入框
var gocheck = document.querySelector(".gocheck");//发送框
var username = document.querySelector("#exampleInputUsername1");
var checkCode = document.querySelector("#exampleInputCheckcode");
var newpsaaword = document.querySelector("#exampleInputNewpassword");

var urlH = "http://localhost:8080";

var goCheckMsg;//接收点击发送 给邮箱发送验证码后返回的信息

//封装函数，获取请求返回信息
function getMsg(url,data){
    var goCheckMsg;
    ajax('get',url,data,function(res){
        if(res.status == 200){
            alert("发送成功");
            goCheckMsg = res;
        }else{
            goCheckMsg = res;
        }
    },false)
    return goCheckMsg;
}
//点击发送按钮 给邮箱发送验证码
gocheck.onclick = function(){
    var url = urlH + "/user/email";
    var data = "?" + "email" + "=" + emailInput.value;
    goCheckMsg = getMsg(url,data);
    // console.log(goCheckMsg);
}
//选中表单
var form = document.getElementsByTagName("form")[0];
submit.onclick = function(e){
    e.preventDefault();
    var url = urlH + "/user/retPassword?";
    console.log(goCheckMsg);
    var data = "email=" + emailInput.value + "&emailCode=" + checkCode.value + "&emailId=" + goCheckMsg.data.token + "&password=" + newpsaaword.value + "&username=" + username.value;
    var formData = getFormData(form);
    if(formData.status == '401'){
        alert(formData.msg);
        console.log(formData);
    }else{
        ajax('post',url,data,function(res){
            if(res.status == 200){
                alert("修改成功");
                window.location.href="../first-web.html";
            }else{
                alert(res.msg);
            }
        },true)
    }
}